package hhs.scannerapp;

import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mail
{
	//variables used for sending email through a gmail account
	private static final String user = "convmailer@gmail.com";
	private static final String pass = "!Synthesis";
	private static final String port = "465";
	private static final String sport = "465";
	private static final String host = "smtp.gmail.com";
	private static final boolean auth = true;;
	private static final boolean debuggable = false;
	
	//used for different file formats when emailing an attachment
	static 
	{
		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
		CommandMap.setDefaultCommandMap(mc);
	}

	//sends email
	public static boolean send(String[] to, String subject, String body, File attachment, String filePrefix) throws Exception 
	{
		//sets properties needed to send email
		Properties props = new Properties();

		props.put("mail.smtp.host", host);
		if (debuggable) 
		{
			props.put("mail.debug", "true");
		}
		if (auth) 
		{
			props.put("mail.smtp.auth", "true");
		}

		props.put("mail.smtp.port", port);
		props.put("mail.smtp.socketFactory.port", sport);
		props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");

		if (to.length > 0 && !subject.equals("") && !body.equals("") && attachment != null) 
		{
			Multipart multipart = new MimeMultipart();
			Session session = Session.getInstance(props, new Authenticator() 
			{
				public PasswordAuthentication getPasswordAuthentication() 
				{
					return new PasswordAuthentication(user, pass);
				}
			});

			MimeMessage msg = new MimeMessage(session);

			//set the sender
			msg.setFrom(new InternetAddress(user));

			//sets receiver
			InternetAddress[] addressTo = new InternetAddress[to.length];
			for (int i = 0; i < to.length; i++) 
			{
				addressTo[i] = new InternetAddress(to[i]);
			}
			msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

			//sets subject
			msg.setSubject(subject);
			//sets date
			msg.setSentDate(new Date());

			// setup message body
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(body);
			multipart.addBodyPart(messageBodyPart);

			BodyPart filePart = new MimeBodyPart();
			DataSource source = new FileDataSource(attachment);
			filePart.setDataHandler(new DataHandler(source));
			String extension = attachment.getName().substring(attachment.getName().indexOf("."));
			//sets attachment
			filePart.setFileName("calls" + extension);
			multipart.addBodyPart(filePart);

			// puts parts in message
			msg.setContent(multipart);

			// send email
			Transport.send(msg);

			//if valid email was sent
			return true;
		} 
		else 
		{
			return false;
		}
	}
}